package com.lizhi.updata;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lizhi.model.BookModel;
import com.lizhi.util.Util;

public class FetchData {

	public static void main(String[] args) throws ClientProtocolException, IOException {
//		System.out.println(System.currentTimeMillis());
//
		String host = "https://freebook.firebaseio.com/books.json";
//		List<BookModel> bookModels = new ArrayList<BookModel>();
//		for (int i = 0; i < 6; i++) {
			String url = host;
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpGet);
			InputStream inputStream = response.getEntity().getContent();
			
			JSONObject jsonObject = JSON.parseObject(Util.readStream(inputStream));
			httpGet.abort();
			httpGet.completed();
			httpClient.close();
	        List<BookModel> bookModels = new ArrayList<BookModel>();
	        Set<String> keySet = jsonObject.keySet();
	        for (String string : keySet) {
	            BookModel bookModel = JSON.toJavaObject(jsonObject.getJSONObject(string), BookModel.class);
	            bookModels.add(bookModel);
	            
	            if(bookModel.chapterModels==null || bookModel.chapterModels.size()<=0){
	            	System.out.println(bookModel.name+" chapter is null!");
	            	continue;
	            }
	            
	            String chapterUrl=bookModel.chapterModels.get(0).url;
	            chapterUrl="https://fbook.firebaseio.com/content"+chapterUrl;
	            CloseableHttpClient httpClient2 = HttpClients.createDefault();
				HttpGet httpGet2 = new HttpGet(chapterUrl);
				CloseableHttpResponse response2 = httpClient2.execute(httpGet2);
				InputStream inputStream2 = response2.getEntity().getContent();
				String content=Util.readStream(inputStream2);
//				System.out.println(chapterUrl);
//				System.out.println(content);
				if(content==null || content.equals("null\n") || content.equals("")){
					System.out.println(bookModel.name+" chapter is null!");
					Util.clearData("https://freebook.firebaseio.com/books/"+string+".json");
				}
	        }
//
//			List<BookModel> tempModels = JSON.parseArray(Util.readStream(inputStream), BookModel.class);
//			bookModels.addAll(tempModels);
//		}
//		int i = 0;
//		long time = System.currentTimeMillis();
//		for (BookModel bookModel : bookModels) {
//			bookModel.time = String.valueOf(time);
//			i++;
//
//			if (i % 10 == 0) {
//				time += 24 * 60 * 60 * 1000;
//			} else {
//				time++;
//			}
//		}
		
//		new UploadDataTask("https://freebook.firebaseio.com/content/articles", Util.readStream(inputStream)).start();
	}

}
